<?php
/**
 * FileName function.html_pager.php
 * Date		2012-8-24
 * Author	jime
 * Mail 	me@joyphper.net
 * QQ		97142822
 * Copyright (C) 2012 joyphper.net
 *
 * Smarty plugin
 * Smarty {html_pager} plugin
 *
 * Type:     function<br>
 * Name:     page<br>
 *
 * 与系统框架相结合的分页展示扩展
 */
function smarty_function_html_pager($params, $template)
{
	$pager 	= $params['pager'];
	$pbreak	= empty($params['pbreak'])?"page":$params['pbreak'];
	$string.="<div class='pager' id='pager'>";
	if ($pager['current']<=1){
		$string.="<span>首页</span>";
		$string.="<span>上一页</span>";
	}else{
		$string.="<a href='".smarty_function_html_pager_builder_url($pager['first'],$pbreak)."'>首页</a>";
		$string.="<a href='".smarty_function_html_pager_builder_url($pager['prev'],$pbreak)."'>上一页</a> ";
	}
	if(count($pager['numbers'])){
		foreach($pager['numbers'] as $_v){
			if ($pager['current']==$_v){
				$string.="<span class='current_page'>$_v</span>";
			}else{
				$string.="<a class='pnum' href='".smarty_function_html_pager_builder_url($_v,$pbreak)."'>$_v</a>";
			}
		}
	}
	if ($pager['current']>=$pager['total']){
		$string.="<span>下一页</span>";
		$string.="<span>末页</span>";
	}else{
		$string.="<a href='".smarty_function_html_pager_builder_url($pager['next'],$pbreak)."'>下一页</a>";
		$string.="<a href='".smarty_function_html_pager_builder_url($pager['last'],$pbreak)."'>末页</a> ";
	}
	$string.="<span>共{$pager['total']}页{$pager['count']}条记录</span>";
	return $string;
}

function smarty_function_html_pager_builder_url($page,$pbreak){
	if(preg_match ("/$pbreak=([0-9]+)/i", $_SERVER["REQUEST_URI"]))
		$pageParm = preg_replace("/$pbreak=([0-9]+)/i","$pbreak={$page}",$_SERVER["REQUEST_URI"]);
	else{
		if(empty($_SERVER['QUERY_STRING']))
			$pageParm =$_SERVER["REQUEST_URI"]."?$pbreak={$page}";
		else
			$pageParm =$_SERVER["REQUEST_URI"]."&$pbreak={$page}";
	}
	return $pageParm;
}
?>
